#pragma once

#include <pthread.h>
#include <iostream>

//锁
namespace MutexModule
{
    class Mutex
    {
    public:
        Mutex()
        {
            pthread_mutex_init(&_Mutex,nullptr);
        }

        void Lock()
        {
            pthread_mutex_lock(&_Mutex);
        }

        void UnLock()
        {
            pthread_mutex_unlock(&_Mutex);
        }

        pthread_mutex_t* Get()
        {
            return &_Mutex;
        }

        ~Mutex()
        {
            pthread_mutex_destroy(&_Mutex);
        }

    private:
        pthread_mutex_t _Mutex;    
    };

    class LockGuard
    {
    public:
        LockGuard(Mutex& mutex)
            :_mutex(mutex)
        {
            _mutex.Lock();
        }
        ~LockGuard()
        {
            _mutex.UnLock();
        }

    private:
        Mutex& _mutex;
    };
}